<script setup lang="ts">
import { ref, computed } from 'vue'
import { useGameStore } from '@/stores/modules/game'
import FilterChefs from './FilterChefs.vue'
import ChefTable from './ChefTable.vue'
import RecipeTable from './RecipeTable.vue'
import FilterRecipes from './FilterRecipes.vue'

import type { ChefSettings, RecipeSettings } from './types'

const chefSettings = ref<ChefSettings>({
  rarity: [5],
  techniques: [],
  condiment: [],
  collect: [],
})

const recipeSettings = ref<RecipeSettings>({
  rarity: [5, 4, 3, 2, 1],
  techniques: [],
  condiment: [],
  material: [],
  rune: [],
})

const chefs = computed(() => {
  return useGameStore().chefs.filter((chef) => {
    const { rarity, techniques, condiment, collect } = chefSettings.value

    const checkRarity = rarity.includes(chef.rarity)
    const checkTechniques = techniques.every((key) => chef[key] > 0)
    const checkCondiment = condiment.every((key) => chef[key] > 0)
    const checkCollect = collect.every((key) => chef[key] > 0)

    return checkRarity && checkTechniques && checkCondiment && checkCollect
  })
})

const recipes = computed(() => {
  return useGameStore().recipes.filter((recipe) => {
    const { rarity, techniques, condiment, material, rune } = recipeSettings.value

    const checkRarity = rarity.includes(recipe.rarity)
    const checkTechniques = techniques.every((key) => recipe[key] > 0)
    const checkCondiment = condiment.every((key) => recipe['condiment'] === key)
    const checkMaterial = material.every((key) => recipe.materials.some((s) => s.material === key))
    const checkRune = rune.length ? rune.some((s) => s === recipe.gift) : true

    return checkRarity && checkTechniques && checkCondiment && checkMaterial && checkRune
  })
})
</script>

<template>
  <n-flex vertical>
    <FilterRecipes v-model:settings="recipeSettings" />
    <RecipeTable :recipes="recipes" />
    <n-divider />
    <FilterChefs v-model:settings="chefSettings" />
    <ChefTable :chefs="chefs" />
  </n-flex>
</template>

<style lang="scss" scoped></style>
